
***************************************************
* start: Figure 2
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */


capture drop time_w
g time_w=0
local w_count=0
forvalues year=2000/2006{
sum week if year==`year'
replace time_w=`w_count'+week if year==`year'
local w_count=`w_count'+r(max)
}

************************************************
* Define 6 months differences
************************************************
xtset pnr time_w
local y="sa"
local y2="wage"
capture drop dif_`y' out_`y' in_`y'
g dif_`y'=`y'-l26.`y' 
g out_`y'=dif_`y'==-1  if dif_`y'!=. 
g in_`y'=dif_`y'==1    if dif_`y'!=.  
capture drop dif_`y2' out_`y2' in_`y2'
g dif_`y2'=`y2'-l26.`y2' 
g out_`y2'=dif_`y2'==-1  if dif_`y2'!=. 
g in_`y2'=dif_`y2'==1    if dif_`y2'!=.  
capture drop e
g e=1

capture drop b_month b_year
g b_month=month(foed_dagl)
g b_year=year(foed_dagl)
local y="sa"
local y2="wage"

************************************************
* Estimate residuals
************************************************

reg dif_`y' i.month##i.year i.b_month##i.b_year if age_q>=21 & age_q<29
capture drop yhat_dif
predict yhat_dif, r
replace dif_`y'=yhat_dif
reg in_`y' i.month##i.year i.b_month##i.b_year if age_q>=21 & age_q<29
capture drop yhat_in
predict yhat_in, r
replace in_`y'=yhat_in
reg out_`y' i.month##i.year i.b_month##i.b_year if age_q>=21 & age_q<29
capture drop yhat_out
predict yhat_out, r
replace out_`y'=yhat_out

reg dif_`y2' i.month##i.year i.b_month##i.b_year if age_q>=21 & age_q<29
capture drop yhat_dif2
predict yhat_dif2, r
replace dif_`y2'=yhat_dif2
reg in_`y2' i.month##i.year i.b_month##i.b_year if age_q>=21 & age_q<29
capture drop yhat_in2
predict yhat_in2, r
replace in_`y2'=yhat_in2
reg out_`y2' i.month##i.year i.b_month##i.b_year if age_q>=21 & age_q<29
capture drop yhat_out2
predict yhat_out2, r
replace out_`y2'=yhat_out2

preserve
local y="sa"
local y2="wage"

keep if age_q>=21 & age_q<29

collapse dif_`y' in_`y' out_`y' dif_`y2' in_`y2' out_`y2' (sem) dif_se=dif_`y' in_se=in_`y' out_se=out_`y' dif_se2=dif_`y2' in_se2=in_`y2' out_se2=out_`y2' (sum) e if  dif_`y'!=. & dif_`y2'!=., by(age_q)

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
g age0_q=age_q-25
g d=age0_q>=0
g dXage0_q=d*age0_q
g age0_q2=age0_q^2
g dXage0_q2=d*age0_q2
g age0_q3=age0_q^3
g dXage0_q3=d*age0_q3
g age0_q4=age0_q^4
g dXage0_q4=d*age0_q4


g dif_lo=dif_`y' - (1.96*dif_se)
g dif_hi=dif_`y' + (1.96*dif_se)
g in_lo=in_`y'   - (1.96*in_se)
g in_hi=in_`y'   + (1.96*in_se)
g out_lo=out_`y' - (1.96*out_se)
g out_hi=out_`y' + (1.96*out_se)

g dif_lo2=dif_`y2' - (1.96*dif_se2)
g dif_hi2=dif_`y2' + (1.96*dif_se2)
g in_lo2 = in_`y2' - (1.96*in_se2)
g in_hi2 = in_`y2' + (1.96*in_se2)
g out_lo2=out_`y2' - (1.96*out_se2)
g out_hi2=out_`y2' + (1.96*out_se2)

local spec ="age_q age_q2 age_q3 age_q4"

local cond="age_month_q<24.12 | age_month_q>25.10"  /*!*/
g     cond=age_month_q>=24.12 & age_month_q<=25.10  /*!*/

g yhat_dif=dif_`y'
reg yhat_dif age0_q age0_q2 age0_q3 age0_q4 [fw=e] if `cond'
capture drop yhat_dif_reg
predict yhat_dif_reg
predict dif_res, r
reg yhat_dif age0_q age0_q2 age0_q3 age0_q4 cond [fw=e] 
capture drop yhat_dif_reg2
predict yhat_dif_reg2

g yhat_in=in_`y'
reg yhat_in age0_q age0_q2 age0_q3 age0_q4 [fw=e] if `cond'
capture drop yhat_in_reg
predict yhat_in_reg
predict in_res, r
reg yhat_in age0_q age0_q2 age0_q3 age0_q4 cond [fw=e] 
capture drop yhat_in_reg2
predict yhat_in_reg2

g yhat_out=out_`y'
reg yhat_out age0_q age0_q2 age0_q3 age0_q4 [fw=e] if `cond'
capture drop yhat_out_reg
predict yhat_out_reg
predict out_res, r
reg yhat_out age0_q age0_q2 age0_q3 age0_q4 cond [fw=e] 
capture drop yhat_out_reg2
predict yhat_out_reg2

g yhat_dif2=dif_`y2'
reg yhat_dif2 age0_q age0_q2 age0_q3 age0_q4 [fw=e] if `cond'
capture drop yhat_dif2_reg
predict yhat_dif2_reg
predict dif2_res, r
reg yhat_dif2 age0_q age0_q2 age0_q3 age0_q4 cond [fw=e] 
capture drop yhat_dif2_reg2
predict yhat_dif2_reg2

g yhat_in2=in_`y2'
reg yhat_in2 age0_q age0_q2 age0_q3 age0_q4 [fw=e] if `cond'
capture drop yhat_in2_reg
predict yhat_in2_reg
predict in2_res, r
reg yhat_in2 age0_q age0_q2 age0_q3 age0_q4 cond [fw=e] 
capture drop yhat_in2_reg2
predict yhat_in2_reg2

g yhat_out2=out_`y2'
reg yhat_out2 age0_q age0_q2 age0_q3 age0_q4 [fw=e] if `cond'
capture drop yhat_out2_reg
predict yhat_out2_reg
predict out2_res, r
reg yhat_out2 age0_q age0_q2 age0_q3 age0_q4 cond [fw=e] 
capture drop yhat_out2_reg2
predict yhat_out2_reg2

collapse yhat_dif yhat_dif_reg yhat_dif_reg2 dif_lo dif_hi  ///
		 yhat_in yhat_in_reg yhat_in_reg2    in_lo in_hi    ///
		 yhat_out yhat_out_reg yhat_out_reg2 out_lo out_hi  ///
		 yhat_dif2 yhat_dif2_reg yhat_dif2_reg2 dif_lo2 dif_hi2  ///
		 yhat_in2 yhat_in2_reg yhat_in2_reg2    in_lo2 in_hi2    ///
		 yhat_out2 yhat_out2_reg yhat_out2_reg2 out_lo2 out_hi2  ///
		 dif_res in_res out_res dif2_res in2_res out2_res  ///
		 [fw=e], by(age_q)  

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local cond_inv="age_month_q>=24.12 & age_month_q<=25.10"  /*!*/

twoway (line yhat_dif_reg age_q, lpattern(shortdash) lcolor(gs4) lwidth(medthick)) (line yhat_dif_reg2 age_q, lpattern(solid)  lcolor(gs2) lwidth(medium)) (rcap dif_lo dif_hi age_q, lwidth(vthin) lcolor(gs10)) (scatter yhat_dif age_q , msymbol(o) mlcolor(gs1)  mfcolor(gs14) msize(medlarge)) (scatter yhat_dif age_q if `cond_inv', msymbol(o) mcolor(gs1) msize(medlarge)) , scheme(s1mono) legend(off) xline(24.95833) xlabel(21(1)29)  /* ylabel(-0.03(.01).03)*/ name(dif_`y', replace)
twoway (line yhat_in_reg age_q , lpattern(shortdash) lcolor(gs4) lwidth(medthick)) (line yhat_in_reg2  age_q, lpattern(solid)  lcolor(gs2) lwidth(medium)) (rcap in_lo  in_hi  age_q, lwidth(vthin) lcolor(gs10)) (scatter yhat_in age_q  , msymbol(o) mlcolor(gs1)  mfcolor(gs14)msize(medlarge)) (scatter yhat_in  age_q if `cond_inv' , msymbol(o) mcolor(gs1) msize(medlarge)) ,  scheme(s1mono) legend(off) xline(24.95833) xlabel(21(1)29) /* ylabel(-0.03(.01).03)*/  name(in_`y', replace)
twoway (line yhat_out_reg age_q, lpattern(shortdash) lcolor(gs4) lwidth(medthick)) (line yhat_out_reg2 age_q, lpattern(solid)  lcolor(gs2) lwidth(medium)) (rcap out_lo out_hi age_q, lwidth(vthin) lcolor(gs10)) (scatter yhat_out age_q , msymbol(o) mlcolor(gs1)  mfcolor(gs14)msize(medlarge)) (scatter yhat_out age_q if `cond_inv', msymbol(o) mcolor(gs1) msize(medlarge))  , scheme(s1mono) legend(off) xline(24.95833) xlabel(21(1)29)  /* ylabel(-0.03(.01).03)*/  name(out_`y', replace)
twoway (line yhat_dif2_reg age_q, lpattern(shortdash) lcolor(gs4) lwidth(medthick)) (line yhat_dif2_reg2 age_q, lpattern(solid)  lcolor(gs2) lwidth(medium)) (rcap dif_lo2 dif_hi2 age_q, lwidth(vthin) lcolor(gs10)) (scatter yhat_dif2 age_q , msymbol(o) mlcolor(gs1)  mfcolor(gs14) msize(medlarge)) (scatter yhat_dif2 age_q if `cond_inv', msymbol(o) mcolor(gs1) msize(medlarge)) , scheme(s1mono) legend(off) xline(24.95833) xlabel(21(1)29)   /*ylabel(-0.03(.01).03)*/ name(dif_`y2', replace)
twoway (line yhat_in2_reg age_q , lpattern(shortdash) lcolor(gs4) lwidth(medthick)) (line yhat_in2_reg2  age_q, lpattern(solid)  lcolor(gs2) lwidth(medium)) (rcap in_lo2  in_hi2  age_q, lwidth(vthin) lcolor(gs10)) (scatter yhat_in2 age_q  , msymbol(o) mlcolor(gs1)  mfcolor(gs14)msize(medlarge)) (scatter yhat_in2  age_q if `cond_inv' , msymbol(o) mcolor(gs1) msize(medlarge)) ,  scheme(s1mono) legend(off) xline(24.95833) xlabel(21(1)29)  /*ylabel(-0.03(.01).03)*/  name(in_`y2', replace)
twoway (line yhat_out2_reg age_q, lpattern(shortdash) lcolor(gs4) lwidth(medthick)) (line yhat_out2_reg2 age_q, lpattern(solid)  lcolor(gs2) lwidth(medium)) (rcap out_lo2 out_hi2 age_q, lwidth(vthin) lcolor(gs10)) (scatter yhat_out2 age_q , msymbol(o) mlcolor(gs1)  mfcolor(gs14)msize(medlarge)) (scatter yhat_out2 age_q if `cond_inv', msymbol(o) mcolor(gs1) msize(medlarge))  , scheme(s1mono) legend(off) xline(24.95833) xlabel(21(1)29)   /*ylabel(-0.03(.01).03)*/  name(out_`y2', replace)

twoway (bar in2_res age_q , barwidth(0.09) fcolor(gs8) lcolor(gs1) lwidth(vthin))  ///
	   (bar out2_res age_q, barwidth(0.09) fcolor(gs16) lcolor(gs1) lwidth(vthin) ) ///
	   (connect dif2_res age_q, msymbol(x) msize(medium) mcolor(gs6) lpattern(solid) lwidth(medthin) lcolor(gs6))                ///
	   , xtitle("Age in months", height(5)) xline(24.958333, lcolor(gs4) lpattern(solid) lwidth(thin))  xlabel(21(1)29) ///
	     ytitle("Rate of change, Employment" "(deviation from trend)",height(8)) note("Six months rate of change") ///
	   scheme(s1mono) legend(order(3 "Total change, Employment" 2 "Transition Out of Employment" 1 "Transition Into Employment") size(small) symxsize(3) lcolor(gs6) lwidth(thin) col(1) position(11) ring(0) bplace(nwest) region(lcolor(none))) ylabel(-0.02(0.01)0.02) name(fig2_1, replace) ///
	   subtitle("Employment", margin(b=4))
graph save fig2_1  $figures/fig2_1, replace
graph export $figures/fig2_1.pdf, name(fig2_1) replace
graph export $figures/fig2_1.png, name(fig2_1) replace

twoway (bar in_res age_q , barwidth(0.09) fcolor(gs16) lcolor(gs1) lwidth(vthin))  ///
	   (bar out_res age_q, barwidth(0.09) fcolor(gs8) lcolor(gs1) lwidth(vthin)) ///
	   (connect dif_res age_q, msymbol(x) msize(medium) mcolor(gs6) lpattern(solid) lwidth(medthin) lcolor(gs6))                ///
	   , xtitle("Age in months", height(5)) xline(24.958333, lcolor(gs4) lpattern(solid) lwidth(thin))  xlabel(21(1)29) ///
	     ytitle("Rate of change, Social Assistance" "(deviation from trend)",height(8)) note("Six months rate of change") ///
	   scheme(s1mono) legend(order(3 "Total change, SA" 1 "Transition Into SA" 2 "Transition Out of SA" ) size(small) symxsize(3) lcolor(gs6) lwidth(thin) col(1) position(7) ring(0) bplace(swest) region(lcolor(none))) ylabel(-0.02(0.01)0.02) name(fig2_2, replace) ///
	   subtitle("Social Assistance", margin(b=4))
graph save fig2_2  $figures/fig2_2, replace
graph export $figures/fig2_2.pdf, name(fig2_2) replace
graph export $figures/fig2_2.png, name(fig2_2) replace

graph use $figures/fig2_1
graph use $figures/fig2_2
graph combine ///
      fig2_1 fig2_2 ///
      , cols(2) rows(2) ysize(4) xsize(10) altshrink  scheme(s1mono) ///
	   name(fig2, replace)
graph save fig2  $figures/fig2, replace
graph export $figures/fig2.pdf, name(fig2) replace
graph export $figures/fig2.png, name(fig2) replace

***************************************************
* end: Figure 2
***************************************************

